package com.kk.player;


import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

public class TrackListFragment extends Fragment {
    private ListView trackList;
    Cursor musiccursor;
	int music_column_index;
	int count;
	MediaPlayer mMediaPlayer;

	public TrackListFragment() {
		mMediaPlayer = new MediaPlayer();
    }

    public static final String ARG_SECTION_NUMBER = "section_number";
    public static final String ARG_SECTION_TITLE = "section_title";

    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	 trackList = new ListView(getActivity());
         String[] proj = {MediaStore.Audio.Media._ID,
         			MediaStore.Audio.Media.DATA,
         			MediaStore.Audio.Media.DISPLAY_NAME,
         			MediaStore.Video.Media.SIZE,
         			MediaStore.Audio.Albums.ALBUM};
 		musiccursor = PlayerUtils.query(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
 		count = musiccursor.getCount();
 		trackList.setAdapter(new MusicAdapter(getActivity()));
 		//trackList.setOnItemClickListener(musicgridlistener);
 		
 		return trackList;
    }
    
    private OnItemClickListener musicgridlistener = new OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position,
				long id) {
			int songIndex = position;
			
			// Starting new intent
			Intent in = new Intent(getActivity(), MusicPlayActivity.class);
			// Sending songIndex to PlayerActivity
			in.putExtra("songIndex", songIndex);
			startActivity(in);
			
			/*music_column_index = musiccursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
			musiccursor.moveToPosition(position);
			String filename = musiccursor.getString(music_column_index);

			try {
				if (mMediaPlayer.isPlaying()) {
					mMediaPlayer.reset();
				}
				mMediaPlayer.setDataSource(filename);
				mMediaPlayer.prepare();
				mMediaPlayer.start();
			} catch (Exception e) {
				System.out.println(e);

			}*/
		}
	};
    public class MusicAdapter extends BaseAdapter {
		private Context mContext;

		public MusicAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return count;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			System.gc();
			GridView gv = new GridView(mContext.getApplicationContext());
			TextView trackName = new TextView(mContext.getApplicationContext());
			TextView album = new TextView(mContext.getApplicationContext());
			if (convertView == null) {
				music_column_index = musiccursor
						.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
				musiccursor.moveToPosition(position);
				trackName.setText(musiccursor.getString(music_column_index));
				music_column_index = musiccursor
						.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
				album.setText(musiccursor.getString(music_column_index));
				gv.addView(trackName);
				gv.addView(album);
				//gv.addView(child)
			} else
				gv = (GridView) convertView;
			return gv;
		}
    }
}
